<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="8" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">智能教学平台</span>
        </div>
        <div class="bottom">
          <div class="container">
            <p class="title">账号登录</p>
            <el-form :label-position="labelPosition" label-width="100px" :model="form">
              <el-form-item label="邮箱地址">
                <el-input v-model="form.email" placeholder="请输入邮箱地址"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
              </el-form-item>
              <el-form-item label="身份选择">
                <el-radio-group v-model="role">
                  <el-radio :label="0">学生</el-radio>
                  <el-radio :label="1">老师</el-radio>
                  <el-radio :label="2">管理员</el-radio>
                </el-radio-group>
              </el-form-item>
              <div class="submit">
                <el-button type="primary" class="row-login" @click="login()">登录</el-button>
              </div>
              <div class="options">
                <p class="find" @click="forget()"><a href="javascript:;">找回密码</a></p>
                <div class="register">
                  <span>没有账号?</span>
                  <span @click="logup()"><a href="javascript:;">去注册</a></span>
                </div>
              </div>
            </el-form>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useCookies } from 'vue3-cookies'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const { cookies } = useCookies()

const form = ref({
  email: '',
  password: ''
})

const role = ref(0)
const labelPosition = ref('left')

// 用户登录请求后台处理
const login = async () => {
  try {
    const res = await axios.post(`/api/customer/login`, {
      email: form.value.email,
      password: form.value.password,
      role: role.value
    })

    if (!res || !res.data) {
      throw new Error('用户名或密码错误')
    }

    const resData = res.data.data
    if (resData.code === "500") {
      throw new Error('用户不存在')
    }

    cookies.set('cname', resData.username)
    cookies.set('cid', resData.id)
    cookies.set('email', resData.email)
    cookies.set('token', resData.token)

    const userRes = await axios.get(`/api/customer/user/id`, {
      params: { id: resData.id }
    })

    if (role.value === userRes.data.data.role) {
      switch (role.value) {
        case 0: // 学生
          cookies.set('role', 0)
          router.push({ path: '/student' })
          break
        case 1: // 教师
          cookies.set('role', 1)
          router.push({ path: '/teacher' })
          break
        case 2: // 管理员
          cookies.set('role', 2)
          router.push({ path: '/admin' })
          break
      }
    } else {
      throw new Error('用户身份权限有误')
    }
  } catch (err) {
    console.error(err)
    ElMessage.error('登录失败')
  }
}

// 用户忘记密码，跳转到forget页面
const forget = () => {
  console.log('点击找回密码')
  router.push({ path: '/forget' })
}

// 用户注册，跳转到logup界面
const logup = () => {
  console.log('点击注册')
  router.push({ path: '/logup' })
}
</script>

<style lang="less" scoped>
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border-left: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 30px 0px;
}
a:link {
  color: #ff962a;
  text-decoration: none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginbg.png')center top / cover no-repeat;
  background-color: #b6bccdd1 !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 100px;
  font-size: 30px;
  color: #ff962a;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display: flex;
  justify-content: center;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #e3e3e3;
  margin-top: 70px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}
</style>
